const path = require("path");
const fs = require("fs");
const dayjs = require("dayjs");
const md5 = require("js-md5");

/**
 * @function noneFolderCreateFolderByUpLoad
 * @desc 检测public中是否存在以年月日命名的文件夹，若存在啥也不干 不存在就创建
 * @return {{realtiveDirPath: string, fileNamePath: string, absoluteDirPath: string}}
 */
module.exports = function noneFolderCreateFolderByUpLoad(req, file) {
  req.app.file = file
  let realtiveDirPath = `/static/${dayjs().format('YYYYMMDD')}`;
  let fileNamePath = '/'+ dayjs().format('HHmmss') + md5(file.originalname) + path.extname(file.originalname)

  let absoluteDirPath = path.join(process.cwd(), realtiveDirPath)

  if (!fs.existsSync(absoluteDirPath)) fs.mkdirSync(absoluteDirPath)

  return {realtiveDirPath, fileNamePath, absoluteDirPath}
}
